<template>
  <div class="box">
    <my-header title="GoodsList案例" color="#fff" />
    <my-table :list="list">
      <template #th>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #td="{ row }">
        <td>{{ row.id }}</td>
        <td>{{ row.goods_name }}</td>
        <td>{{ row.goods_price }}</td>
        <td>
          <input
            v-focus
            v-if="row.inputVisible"
            v-model.trim="row.inputValue"
            @keydown.esc="row.inputVisible = false"
            @keydown.enter="add(row)"
            type="text"
            name=""
            id=""
          />
          <button @click="row.inputVisible = true;" v-else class="btn btn-primary btn-sm">
            Tag
          </button>
          <br />
          <span class="badge bg-warning text-dark" v-for="item in row.tags">
            {{ item }}
          </span>
        </td>
        <td>
          <button @click="del(row.id)" class="btn btn-danger btn-sm">
            删除
          </button>
        </td>
      </template>
    </my-table>
  </div>
</template>

<script>
import MyHeader from "components/myHeader/MyHeader";
import MyTable from "components/myTable/MyTable";

export default {
  data() {
    return {
      list: [],
    };
  },
  components: {
    MyHeader,
    MyTable,
  },
  async created() {
    const res = await this.$axios.get("api/goods");
    console.log(res.data.data);
    this.list = res.data.data;
  },
  methods: {
    del(id) {
      const index = this.list.findIndex((item) => (item.id = id));
      this.list.splice(index, 1);
      this.$prompt.show('删除成功')
    },
    add(row) {
      if (row.inputValue.length === 0) return this.$prompt.show('请输入名称')
      row.tags.push(row.inputValue)
      this.$prompt.show('添加成功')
      row.inputValue = ''
    },
  },
};
</script>

<style lang="less" scoped>
.box {
  padding-top: 45px;
}
.badge {
  margin-right: 8px;
}
</style>